﻿using System;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using KeZhou.ViewModels;
using KeZhou.Views.Bindings;
using KeZhou.Views.Components.Customs;
using MahApps.Metro.Controls;

namespace KeZhou.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        
        public MainWindow()
        {
            DataContext = PageExists.Instance;
            InitializeComponent();
            loginControl.SwitchControl += OnSwitchControl;
            signUpControl.SwitchControl += OnSwitchControl;
            resetPasswordControl.SwitchControl += OnSwitchControl;
        }

        //自定义拖动
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        //自定义关闭
        private void WindowCloseFuc(object sender, RoutedEventArgs e)
        {
            myWindowClose(1);
        }

        private void myWindowClose(int time)
        {
            PageExists.Instance.LoginWindosIsClosing = true;
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(time);
            timer.Tick += (s, args) =>
            {
                this.Close();
                timer.Stop();
            };
            timer.Start();
        }

        //登录、注册、找回密码切换
        private void OnSwitchControl(object sender, ControlType controlType)
        {
            loginControl.Visibility = controlType == ControlType.Login ? Visibility.Visible : Visibility.Collapsed;
            signUpControl.Visibility = controlType == ControlType.SignUp ? Visibility.Visible : Visibility.Collapsed;
            resetPasswordControl.Visibility = controlType == ControlType.ResetPassword ? Visibility.Visible : Visibility.Collapsed;
        }

        //登陆弹窗展示信息 
        // 0：手机号未通过正则表达式
        // 1：获取验证码出错
        // 2：输入框有空值
        // 3：两次密码不一致
        // 4：验证码错误
        // 5：注册时手机号已经注册过
        // 6：登录或找回密码时手机号未注册
        // 7：密码错误
        // 8：提示注册失败
        // 9：提示更改密码失败
        // 10：提示登陆失败
        // 11：提示注册成功
        // 12：提示更改密码成功
        // 13：登陆成功，窗口跳转
        public void showWrongMsg(int wrongCode)
        {
            switch (wrongCode)
            {
                case 0:
                    showMyWarn("请输入正确的手机号");
                    break;
                case 1:
                    showMyWarn("获取验证码时出错");
                    break;
                case 2:
                    showMyWarn("请输入完整的账号密码等信息");
                    break;
                case 3:
                    showMyWarn("两次输入的密码不一致");
                    break;
                case 4:
                    showMyWarn("验证码错误");
                    break;
                case 5:
                    showMyWarn("该手机号已经被注册");
                    break;
                case 6:
                    showMyWarn("该手机号还未注册");
                    break;
                case 7:
                    showMyWarn("密码错误");
                    break;
                case 8:
                    showMyWarn("服务器响应错误，注册失败");
                    break;
                case 9:
                    showMyWarn("服务器响应错误，更改密码失败");
                    break;
                case 10:
                    showMyWarn("服务器响应错误，登录失败");
                    break;
                case 11:
                    showMyWarn("注册成功");
                    break;
                case 12:
                    showMyWarn("更改密码成功");
                    break;
                case 13:
                    showMyWarn("登录成功");
                    break;
                default:
                    showMyWarn("发生未知错误");
                    break;
            }
        }

        //展示弹窗方法
        private void showMyWarn(string warnMsg)
        {
            MyWarnBox myWarnBox = new MyWarnBox();
            myWarnBox.Owner = this;
            myWarnBox.Show(warnMsg);
        }

        //获取帮助（联系客服）
        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Clipboard.SetText("1779374060@qq.com");
            showMyWarn("客服邮箱已经复制到剪切板");
        }

        //登陆成功，传参并开启新窗口
        public void pageJump()
        {
            AppDetailView appDetailView = new AppDetailView();
            myWindowClose(2);
            appDetailView.Show();
        }
    }
}
